cmake_minimum_required(VERSION 3.16)

project(Chores VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

if (WIN32)
    set(LIBS_DIR deps/lib/win/)
elseif (UNIX)
    set(LIBS_DIR deps/lib/unix)
endif ()

set(mdx_DIR "${CMAKE_CURRENT_SOURCE_DIR}/modules/mdx/cmake")
find_package(mdx REQUIRED)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Sql)
# find_package(Qt${QT_VERSION_MAJOR} COMPONENTS WebEngineWidgets QUIET)

# 定义宏，供代码中判断模块是否存在
if(Qt${QT_VERSION_MAJOR}WebEngineWidgets_FOUND)
    add_definitions(-DHasWebEngine)
    message(STATUS "Qt WebEngineWidgets 模块已找到，将启用相关功能")
else()
    message(WARNING "Qt WebEngineWidgets 模块未找到，相关功能将禁用")
endif()

set(CHORES_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        utils.cpp
        video/bsitedownloaddialog.h
        video/bsitedownloaddialog.cpp
        video/bsitedownloaddialog.ui
        video/videofactory.cpp
        word/wordtab.h word/wordtab.cpp
        word/hanwidget.h word/hanwidget.cpp
        word/FlyWidget.cpp word/FlyWidget.h
        word/wordframe.h word/wordframe.cpp word/wordframe.ui
        word/DictEngin.cpp
        word/LookupWidget.cpp word/LookupWidget.ui
        word/SearchOverlay.cpp word/SearchOverlay.h
)

# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:>DLL")
find_package(Qt6 REQUIRED COMPONENTS Widgets)

add_executable(Chores ${CHORES_SOURCES}
    static.qrc
    word/bezierwave.h word/bezierwave.cpp)
target_precompile_headers(Chores PRIVATE stable.h)

target_include_directories(Chores
    PRIVATE
    video
    word
    .)
target_link_directories(Chores PRIVATE ${LIBS_DIR})
target_link_directories(Chores PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/mdict-cpp/lib)
target_link_libraries(Chores
    PRIVATE
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Sql
    # $<$<BOOL:${Qt${QT_VERSION_MAJOR}WebEngineWidgets_FOUND}>:Qt${QT_VERSION_MAJOR}::WebEngineWidgets>
    # mdict
    mdx::mdx
    qhotkey
    cfg
    ui
    QLiteHtmlBrowser
)
target_link_libraries(Chores PRIVATE Qt6::Widgets)

install(TARGETS Chores
    BUNDLE  DESTINATION bundle
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# 告诉 CMake 这是 GUI 程序
if(WIN32)
    set_target_properties(Chores PROPERTIES
                          WIN32_EXECUTABLE TRUE   # ← 关键
                          LINK_FLAGS "/ENTRY:mainCRTStartup") # 可选，去掉控制台入口
endif()

if(QT_VERSION_MAJOR GREATER_EQUAL 6)
qt_generate_deploy_app_script(
    TARGET Chores
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
else()
endif()

# 构建完成之后的阶段执行
add_custom_command(TARGET Chores POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_SOURCE_DIR}/Res/coca
            $<TARGET_FILE_DIR:Chores>/coca
)

# 1. 声明依赖
include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        v1.17.0          # 选一个稳定 tag 或 commit
)
# 2. 让 gtest 在编译时自动下载
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# 关键：禁止 gtest 安装它自己的文件
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(INSTALL_GMOCK OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# add_subdirectory(test)
# add_subdirectory(mdict-cpp)
add_subdirectory(modules)
# add_subdirectory(word/cpp_implementation)
